import { sendMessage } from '../../helpers/utils'
import ContactsAPI from '../../api/contacts'
import { SET_USER_ERROR } from '../../constants/errorTypes'
import { setHeader } from '../../helpers/axios'
const state = {
  currentUser: {}
}

const SET_CURRENT_USER = 'SET_CURRENT_USER'
const parseErrorData = error =>
  error && error.response && error.response.data ? error.response.data : error
export const updateWidgetAuthToken = widgetAuthToken => {
  if (widgetAuthToken) {
    setHeader(widgetAuthToken)
    sendMessage({
      event: 'setAuthCookie',
      data: { widgetAuthToken }
    })
  }
}

export const getters = {
  getCurrentUser(_state) {
    return _state.currentUser
  }
}

export const actions = {
  get: async({ commit }) => {
    try {
      const { data } = await ContactsAPI.get()
      commit(SET_CURRENT_USER, data)
    } catch (error) {
      // Ignore error
    }
  },
  update: async({ dispatch }, { user }) => {
    try {
      await ContactsAPI.update(user)
      dispatch('get')
    } catch (error) {
      // Ignore error
    }
  },
  setUser: async({ dispatch }, { identifier, user: userObject }) => {
    try {
      const {
        email,
        name,
        avatar_url,
        identifierHash: identifierHash,
        phoneNumber,
        companyName,
        city,
        countryCode,
        description,
        customAttributes,
        socialProfiles
      } = userObject
      const user = {
        email,
        name,
        avatar_url,
        identifierHash: identifierHash,
        phoneNumber,
        additionalAttributes: {
          companyName,
          city,
          description,
          countryCode,
          socialProfiles
        },
        customAttributes
      }
      const {
        data: { widget_auth_token: widgetAuthToken }
      } = await ContactsAPI.setUser(identifier, user)
      updateWidgetAuthToken(widgetAuthToken)
      dispatch('get')
      if (identifierHash || widgetAuthToken) {
        dispatch('conversation/clearConversations', {}, { root: true })
        dispatch('conversation/fetchOldConversations', {}, { root: true })
        dispatch('conversationAttributes/getAttributes', {}, { root: true })
      }
    } catch (error) {
      const data = parseErrorData(error)
      sendMessage({ event: 'error', errorType: SET_USER_ERROR, data })
    }
  },
  setCustomAttributes: async(_, customAttributes = {}) => {
    try {
      await ContactsAPI.setCustomAttributes(customAttributes)
    } catch (error) {
      // Ignore error
    }
  },
  deleteCustomAttribute: async(_, customAttribute) => {
    try {
      await ContactsAPI.deleteCustomAttribute(customAttribute)
    } catch (error) {
      // Ignore error
    }
  }
}

export const mutations = {
  [SET_CURRENT_USER]($state, user) {
    const { currentUser } = $state
    $state.currentUser = { ...currentUser, ...user }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
